#ifndef SEARCH_H
#define SEARCH_H

#include <QWidget>  // 只需要包含直接基类
#include <QPainter>
#include <QColor>
#include <QLabel>
#include <QHBoxLayout>
#include <QLineEdit>
#include <QLabel>
#include "weather_json.h"

#include <QString>
#include <QMap>

class search : public QWidget
{
    Q_OBJECT
public:
    search();
    explicit search(QWidget *parent = nullptr);
private slots:
    void onTextChanged(const QString &text);
private:
    QLineEdit *searchEdit;
    QLabel *titleLabel;
//转发信号
signals:
    void weatherDataUpdated(const QVector<WeatherData> &weatherData);

private slots:
    void onWeatherDataUpdated(const QVector<WeatherData> &weatherData);
//转发信号结束
private:
    weather_json *network_1;
private:
    QColor color;
    // 设置背景
    void set_color(QColor color);
    // 绘制事件
    void paintEvent(QPaintEvent *event) override;
    //对应编码
    QString to_int(QString city)
    {
        QString normalizedCity = city.trimmed();

                // 查找城市代码
                if (cityCodeMap.contains(normalizedCity)) {
                    qDebug()<<"找到"<<cityCodeMap[normalizedCity];
                    return QString::number(cityCodeMap[normalizedCity]);
                }

                // 如果找不到，返回-1表示未找到
                qDebug()<<"未找到城市";
                return "null";
    }
private:
    QMap<QString, int> cityCodeMap;
private:
    void initializeCityCodes() {
        // 添加所有城市代码映射关系
        cityCodeMap["北京"] = 101010100;
        cityCodeMap["上海"] = 101020100;
        cityCodeMap["天津"] = 101030100;
        cityCodeMap["重庆"] = 101040100;
        cityCodeMap["香港"] = 101320101;
        cityCodeMap["澳门"] = 101330101;
        cityCodeMap["合肥"] = 101220101;
        cityCodeMap["长丰县"] = 101220102;
        cityCodeMap["肥东县"] = 101220103;
        cityCodeMap["肥西县"] = 101220104;
        cityCodeMap["巢湖市"] = 101220105;
        cityCodeMap["庐江县"] = 101220106;
        cityCodeMap["安庆"] = 101220601;
        cityCodeMap["桐城市"] = 101220609;
        cityCodeMap["怀宁县"] = 101220605;
        cityCodeMap["潜山县"] = 101220604;
        cityCodeMap["太湖县"] = 101220603;
        cityCodeMap["宿松县"] = 101220606;
        cityCodeMap["望江县"] = 101220607;
        cityCodeMap["岳西县"] = 101220608;
        cityCodeMap["蚌埠"] = 101220201;
        cityCodeMap["怀远县"] = 101220202;
        cityCodeMap["固镇县"] = 101220203;
        cityCodeMap["五河县"] = 101220204;
        cityCodeMap["芜湖市"] = 101220301;
        cityCodeMap["芜湖县"] = 101220303;
        cityCodeMap["繁昌县"] = 101220302;
        cityCodeMap["南陵县"] = 101220304;
        cityCodeMap["无为县"] = 101220305;
        cityCodeMap["淮南"] = 101220401;
        cityCodeMap["凤台县"] = 101220402;
        cityCodeMap["潘集区"] = 101220403;
        cityCodeMap["寿县"] = 101220408;
        cityCodeMap["马鞍山"] = 101220501;
        cityCodeMap["当涂县"] = 101220502;
        cityCodeMap["含山县"] = 101220503;
        cityCodeMap["和县"] = 101220504;
        cityCodeMap["宿州"] = 101220701;
        cityCodeMap["砀山县"] = 101220702;
        cityCodeMap["萧县"] = 101220705;
        cityCodeMap["灵璧县"] = 101220703;
        cityCodeMap["泗县"] = 101220704;
        cityCodeMap["阜阳"] = 101220801;
        cityCodeMap["阜南县"] = 101220802;
        cityCodeMap["颍上县"] = 101220803;
        cityCodeMap["临泉县"] = 101220804;
        cityCodeMap["界首市"] = 101220805;
        cityCodeMap["太和县"] = 101220806;
        cityCodeMap["亳州"] = 101220901;
        cityCodeMap["涡阳县"] = 101220902;
        cityCodeMap["利辛县"] = 101220903;
        cityCodeMap["蒙城县"] = 101220904;
        cityCodeMap["滁州"] = 101221101;
        cityCodeMap["凤阳县"] = 101221102;
        cityCodeMap["明光市"] = 101221103;
        cityCodeMap["定远县"] = 101221104;
        cityCodeMap["全椒县"] = 101221105;
        cityCodeMap["来安县"] = 101221106;
        cityCodeMap["天长市"] = 101221107;
        cityCodeMap["淮北"] = 101221201;
        cityCodeMap["濉溪县"] = 101221202;
        cityCodeMap["铜陵"] = 101221301;
        cityCodeMap["义安区"] = 101221303;
        cityCodeMap["枞阳县"] = 101221305;
        cityCodeMap["宣城"] = 101221401;
        cityCodeMap["泾县"] = 101221402;
        cityCodeMap["旌德县"] = 101221403;
        cityCodeMap["宁国市"] = 101221404;
        cityCodeMap["绩溪县"] = 101221405;
        cityCodeMap["广德县"] = 101221406;
        cityCodeMap["郎溪县"] = 101221407;
        cityCodeMap["六安"] = 101221501;
        cityCodeMap["霍邱县"] = 101221502;
        cityCodeMap["寿县"] = 101220408;
        cityCodeMap["金寨县"] = 101221505;
        cityCodeMap["霍山县"] = 101221506;
        cityCodeMap["舒城县"] = 101221507;
        cityCodeMap["池州"] = 101221701;
        cityCodeMap["东至县"] = 101221702;
        cityCodeMap["青阳县"] = 101221703;
        cityCodeMap["石台县"] = 101221705;
        cityCodeMap["黄山市"] = 101221001;
        cityCodeMap["黄山区"] = 101221002;
        cityCodeMap["屯溪区"] = 101221003;
        cityCodeMap["祁门县"] = 101221004;
        cityCodeMap["黟县"] = 101221005;
        cityCodeMap["歙县"] = 101221006;
        cityCodeMap["休宁县"] = 101221007;
        cityCodeMap["广州"] = 101280101;
        cityCodeMap["深圳"] = 101280601;
        cityCodeMap["杭州"] = 101210101;
        cityCodeMap["南京"] = 101190101;
        cityCodeMap["长沙"] = 101250101;
    }
};

#endif // SEARCH_H
